//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include "Strings.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 50000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* nameInFile;
  String* nameOutFile;

  if (argc < 3) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./ConvertFASTAToSMI <input file - FASTA format> <output file>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "Input File (FASTA) : " << argv[1] << endl;
    cout << "       Output File : " << argv[2] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    nameInFile  = new String(argv[1]);
    nameOutFile = new String(argv[2]);
  }

  fstream inFile, outFile;

  String* line   = new String("");
  String* record = new String("");
  String* seq    = new String("");

  bool    flag   = true;
  int     count  = 0;

  char    buffer[LENGTHLINE + 1];

  inFile.open(nameInFile->Out(),   fstream::in);
  outFile.open(nameOutFile->Out(), fstream::out);

  while (!inFile.eof()) {
    inFile.getline(buffer, LENGTHLINE);
    flag = (strstr(buffer, ">"))?true:false;
    if (flag) {
      if (line->Length())
        outFile << seq->Out() << record->Out() << endl;
      line->In(buffer); line->ITrim();
      record->In(line->Out());
      record->In(' ', 0);
      seq->In("");
    }
    else {
      line->In(buffer); line->RTrim();
      seq->Add(line);
    }
    count++;
  }

  inFile.close();
  outFile.close();

  cout << "---------------------------------------------------------------------------------" << endl;

  if (line)        delete line;
  if (record)      delete record;
  if (seq)         delete seq;
  if (nameInFile)  delete nameInFile;
  if (nameOutFile) delete nameOutFile;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//


